﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GrandMan.Models;
using GrandMan.GMDataSetTableAdapters;
using GrandMan.Models.Admin;

namespace GrandMan.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/

        public ActionResult Index()
        {
            if (Session["User"] == null)
                return this.RedirectToAction("Login", "Admin");

            //var context = ContextMgt.GetInstance();
            //ArticleTypeTableAdapter adapter = new ArticleTypeTableAdapter();
            //adapter.Fill(context.ArticleType);

            //var list = (from at in context.ArticleType
            //            select at).ToList();

            return View();
        }

        public ActionResult Login()
        {
            LoginModel model = new LoginModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult Login(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.UserName.ToLower() == "admin")
                {
                    var context = ContextMgt.GetInstance();
                    new BaseDataTableAdapter().Fill(context.BaseData);
                    var password = (from p in context.BaseData
                                    where p.Name == "Password"
                                    select p).SingleOrDefault();
                    if (password != null && !password.IsValueNull() && password.Value != model.Password.ToLower())
                    {
                        ModelState.AddModelError("", "密码不正确。");
                        return View(model);
                    }

                    Session["User"] = model.UserName;
                    Session["Menu"] = new MenuModel();
                    return this.RedirectToAction("Index", "Admin");
                }
                ModelState.AddModelError("", "用户名不正确。");
            }
            return View(model);
        }

        public ActionResult Logout()
        {
            Session["User"] = null;
            Session["Menu"] = null;
            return this.RedirectToAction("Login", "Admin");
        }

        //public ActionResult Company()
        //{
        //    if (Session["User"] == null)
        //        return this.RedirectToAction("../Admin/Login");

        //    MenuModel menu = Session["Menu"] as MenuModel;
        //    menu.Select("公司简介");

        //    var context = ContextMgt.GetInstance();
        //    new ArticleTableAdapter().Fill(context.Article);
        //    var list = (from s in context.Article
        //                where s.TypeID == 1
        //                orderby s.Seq
        //                select s).ToList();
        //    return View(list);

        //}

        public ActionResult Company()
        {
            if (Session["User"] == null)
                return this.RedirectToAction("../Admin/Login");

            MenuModel menu = Session["Menu"] as MenuModel;
            menu.Select("公司简介");

            ArticleEditModel model = new ArticleEditModel();
            var context = ContextMgt.GetInstance();


            new ArticleTableAdapter().Fill(context.Article);

            var article = (from a in context.Article
                               where a.TypeID == 1
                               select a).FirstOrDefault();
            if (article == null)
            {
                model.Name = "公司简介";
            }
            else
            {
                model.Name = article.Name;
                model.Seq = article.Seq;
                model.ID = article.ID;
                model.Content = article.Content;
            }

            model.Type = 1;
            return View(model);
        }

        [HttpPost]
        public ActionResult Company(ArticleEditModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var context = ContextMgt.GetInstance();
                    new ArticleTableAdapter().Fill(context.Article);
                    var article = (from a in context.Article
                                   where a.TypeID == 1
                                   select a).FirstOrDefault();
                    if (article == null)
                    {
                        article = context.Article.NewArticleRow();
                        context.Article.AddArticleRow(article);
                    }

                    article.Name = model.Name;
                    article.Seq = model.Seq;
                    article.TypeID = model.Type;
                    article.Content = model.Content;

                    new ArticleTableAdapter().Update(context.Article);
                    context.Article.AcceptChanges();

                    return this.RedirectToAction("Index", "Admin");
                }
                catch (Exception e)
                {
                    return this.RedirectToAction("Error", "Admin", new { message = e.Message });
                }
            }
            return View(model);
        }

        public ActionResult Team()
        {
            if (Session["User"] == null)
                return this.RedirectToAction("../Admin/Login");

            MenuModel menu = Session["Menu"] as MenuModel;
            menu.Select("专家团队");

            var context = ContextMgt.GetInstance();
            new ArticleTableAdapter().Fill(context.Article);
            var list = (from s in context.Article
                        where s.TypeID == 2
                        orderby s.Seq
                        select s).ToList();
            return View(list);

        }

        //public ActionResult Contact()
        //{
        //    if (Session["User"] == null)
        //        return this.RedirectToAction("../Admin/Login");

        //    MenuModel menu = Session["Menu"] as MenuModel;
        //    menu.Select("联系我们");

        //    var context = ContextMgt.GetInstance();
        //    new ArticleTableAdapter().Fill(context.Article);
        //    var list = (from s in context.Article
        //                where s.TypeID == 3
        //                orderby s.Seq
        //                select s).ToList();
        //    return View(list);

        //}

        public ActionResult Contact()
        {
            if (Session["User"] == null)
                return this.RedirectToAction("../Admin/Login");

            MenuModel menu = Session["Menu"] as MenuModel;
            menu.Select("公司简介");

            ArticleEditModel model = new ArticleEditModel();
            var context = ContextMgt.GetInstance();
            new ArticleTableAdapter().Fill(context.Article);

            var article = (from a in context.Article
                           where a.TypeID == 3
                           select a).FirstOrDefault();
            if (article == null)
            {
                model.Name = "联系我们";
            }
            else
            {
                model.Name = article.Name;
                model.Seq = article.Seq;
                model.ID = article.ID;
                model.Content = article.Content;
            }

            model.Type = 3;
            return View(model);
        }

        [HttpPost]
        public ActionResult Contact(ArticleEditModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var context = ContextMgt.GetInstance();
                    new ArticleTableAdapter().Fill(context.Article);
                    var article = (from a in context.Article
                                   where a.TypeID == 3
                                   select a).FirstOrDefault();
                    if (article == null)
                    {
                        article = context.Article.NewArticleRow();
                        context.Article.AddArticleRow(article);
                    }

                    article.Name = model.Name;
                    article.Seq = model.Seq;
                    article.TypeID = model.Type;
                    article.Content = model.Content;

                    new ArticleTableAdapter().Update(context.Article);
                    context.Article.AcceptChanges();

                    return this.RedirectToAction("Index", "Admin");
                }
                catch (Exception e)
                {
                    return this.RedirectToAction("Error", "Admin", new { message = e.Message });
                }
            }
            return View(model);
        }

        public ActionResult Case()
        {
            if (Session["User"] == null)
                return this.RedirectToAction("../Admin/Login");

            MenuModel menu = Session["Menu"] as MenuModel;
            menu.Select("成功案例");

            var context = ContextMgt.GetInstance();
            new ArticleTableAdapter().Fill(context.Article);
            var list = (from s in context.Article
                        where s.TypeID == 6
                        orderby s.Seq
                        select s).ToList();
            return View(list);

        }

        public ActionResult ArticleEdit(int type, int? subType, int? id)
        {
            ArticleEditModel model = new ArticleEditModel();
            var context = ContextMgt.GetInstance();

            if (id != null)
            {
                new ArticleTableAdapter().Fill(context.Article);

                var article = (from a in context.Article
                               where a.ID == id
                               select a).SingleOrDefault();
                if (article == null)
                {
                    return this.RedirectToAction("Error", "Admin", new { message = "没有找到文章！" });
                }
                model.Name = article.Name;
                model.Seq = article.Seq;
                model.IsHome = article.IsHome;
                model.ID = article.ID;
                model.HomeSeq = article.HomeSeq;
                model.Content = article.Content;
            }
            else
            {
                model.ID = -1;
            }
            model.Type = type;
            model.SubType = subType;
            return View(model);
        }

        [HttpPost]
        public ActionResult ArticleEdit(ArticleEditModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    GrandMan.GMDataSet.ArticleRow article = null;
                    var context = ContextMgt.GetInstance();

                    if (model.ID == -1)
                    {
                        article = context.Article.NewArticleRow();
                        context.Article.AddArticleRow(article);
                    }
                    else
                    {
                        new ArticleTableAdapter().Fill(context.Article);
                        article = (from a in context.Article
                                   where a.ID == model.ID
                                   select a).SingleOrDefault();
                        if (article == null)
                        {
                            return this.RedirectToAction("Error", "Admin", new { message = "没有找到文章！" });
                        }
                    }
                    article.Name = model.Name;
                    article.Seq = model.Seq;
                    article.IsHome = model.IsHome;
                    if (model.SubType != null)
                        article.TypeID = model.SubType.Value;
                    else
                        article.TypeID = model.Type;
                    article.HomeSeq = model.HomeSeq;
                    article.Content = model.Content;

                    new ArticleTableAdapter().Update(context.Article);
                    context.Article.AcceptChanges();

                    switch (model.Type)
                    {
                        case 1:
                            return this.RedirectToAction("Company", "Admin");
                        case 2:
                            return this.RedirectToAction("Team", "Admin");
                        case 3:
                            return this.RedirectToAction("Contact", "Admin");
                        case 4:
                            return this.RedirectToAction("NewsSub", "Admin", new { id = model.SubType.Value });
                        case 5:
                            return this.RedirectToAction("InfoSub", "Admin", new { id = model.SubType.Value });
                        default:
                            return this.RedirectToAction("Index", "Admin");
                    }
                }
                catch (Exception e)
                {
                    return this.RedirectToAction("Error", "Admin", new { message = e.Message });
                }
            }
            return View(model);
        }

        public ActionResult Error(string message)
        {
            return View(new MessageModel() { Message = message });
        }

        public ActionResult ArticleDelete(int type, int? subType, string ids)
        {
            string[] temp = ids.Split('!');
            int[] idList = new int[temp.Length];
            for (int i = 0; i < temp.Length; i++)
                idList[i] = Convert.ToInt32(temp[i]);
            try
            {
                var adapter = new ArticleTableAdapter();
                foreach (var item in idList)
                    adapter.Delete(item);
            }
            catch (Exception e)
            {
                return this.RedirectToAction("Error", "Admin", new { message = e.Message });
            }
            switch (type)
            {
                case 1:
                    return this.RedirectToAction("Company", "Admin");
                case 2:
                    return this.RedirectToAction("Team", "Admin");
                case 3:
                    return this.RedirectToAction("Contact", "Admin");
                case 4:
                    return this.RedirectToAction("NewsSub", "Admin", new { id = subType.Value });
                case 5:
                    return this.RedirectToAction("InfoSub", "Admin", new { id = subType.Value });
                default:
                    return this.RedirectToAction("Index", "Admin");
            }
        }

        public ActionResult ArticleTypeEdit(int type, int? id)
        {
            ArticleTypeEditModel model = new ArticleTypeEditModel();
            var context = ContextMgt.GetInstance();

            if (id != null)
            {
                new ArticleTypeTableAdapter().Fill(context.ArticleType);

                var articleType = (from a in context.ArticleType
                                   where a.ID == id
                                   select a).SingleOrDefault();
                if (articleType == null)
                {
                    return this.RedirectToAction("Error", "Admin", new { message = "没有找到子分栏！" });
                }
                model.Name = articleType.Name;
                model.Seq = articleType.Seq;
                model.ID = articleType.ID;
            }
            else
            {
                model.ID = -1;
            }
            model.Type = type;
            return View(model);
        }

        [HttpPost]
        public ActionResult ArticleTypeEdit(ArticleTypeEditModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    GrandMan.GMDataSet.ArticleTypeRow articleType = null;
                    var context = ContextMgt.GetInstance();

                    if (model.ID == -1)
                    {
                        articleType = context.ArticleType.NewArticleTypeRow();
                        context.ArticleType.AddArticleTypeRow(articleType);
                    }
                    else
                    {
                        new ArticleTypeTableAdapter().Fill(context.ArticleType);
                        articleType = (from a in context.ArticleType
                                       where a.ID == model.ID
                                       select a).SingleOrDefault();
                        if (articleType == null)
                        {
                            return this.RedirectToAction("Error", "Admin", new { message = "没有找到子分栏！" });
                        }
                    }
                    articleType.Name = model.Name;
                    articleType.Seq = model.Seq;
                    articleType.ParentID = model.Type;

                    new ArticleTypeTableAdapter().Update(context.ArticleType);
                    context.ArticleType.AcceptChanges();

                    switch (model.Type)
                    {
                        case 1:
                            return this.RedirectToAction("Company", "Admin");
                        case 2:
                            return this.RedirectToAction("Team", "Admin");
                        case 3:
                            return this.RedirectToAction("Contact", "Admin");
                        case 4:
                            return this.RedirectToAction("News", "Admin");
                        case 5:
                            return this.RedirectToAction("Info", "Admin");
                        default:
                            return this.RedirectToAction("Index", "Admin");
                    }
                }
                catch (Exception e)
                {
                    return this.RedirectToAction("Error", "Admin", new { message = e.Message });
                }
            }
            return View(model);
        }

        public ActionResult ArticleTypeDelete(int type, string ids)
        {
            string[] temp = ids.Split('!');
            int[] idList = new int[temp.Length];
            for (int i = 0; i < temp.Length; i++)
                idList[i] = Convert.ToInt32(temp[i]);
            try
            {
                var adapter = new ArticleTypeTableAdapter();
                foreach (var item in idList)
                    adapter.Delete(item);
            }
            catch (Exception e)
            {
                return this.RedirectToAction("Error", "Admin", new { message = e.Message });
            }
            switch (type)
            {
                case 1:
                    return this.RedirectToAction("Company", "Admin");
                case 2:
                    return this.RedirectToAction("Team", "Admin");
                case 3:
                    return this.RedirectToAction("Contact", "Admin");
                case 4:
                    return this.RedirectToAction("News", "Admin");
                case 5:
                    return this.RedirectToAction("Info", "Admin");
                default:
                    return this.RedirectToAction("Index", "Admin");
            }
        }

        public ActionResult News()
        {
            if (Session["User"] == null)
                return this.RedirectToAction("../Admin/Login");

            MenuModel menu = Session["Menu"] as MenuModel;
            menu.Select("最新动态");

            var context = ContextMgt.GetInstance();
            new ArticleTypeTableAdapter().Fill(context.ArticleType);
            var list = (from s in context.ArticleType
                        where s.ParentID == 4
                        orderby s.Seq
                        select s).ToList();
            return View(list);

        }

        public ActionResult NewsSub(int id)
        {
            if (Session["User"] == null)
                return this.RedirectToAction("../Admin/Login");

            MenuModel menu = Session["Menu"] as MenuModel;
            menu.Select("最新动态");

            SubTypeEditModel model = new SubTypeEditModel();

            var context = ContextMgt.GetInstance();
            new ArticleTableAdapter().Fill(context.Article);
            new ArticleTypeTableAdapter().Fill(context.ArticleType);
            var list = (from s in context.Article
                        where s.TypeID == id
                        orderby s.Seq
                        select s).ToList();
            var articleType = (from s in context.ArticleType
                               where s.ID == id
                               orderby s.Seq
                               select s).SingleOrDefault();
            if (articleType == null)
            {
                return this.RedirectToAction("Error", "Admin", new { message = "没有找到子分栏！" });
            }
            model.List = list;
            model.Type = articleType.ParentID;
            model.SubType = id;
            model.TypeName = articleType.Name;
            return View(model);

        }

        public ActionResult Info()
        {
            if (Session["User"] == null)
                return this.RedirectToAction("../Admin/Login");

            MenuModel menu = Session["Menu"] as MenuModel;
            menu.Select("行业资信");

            var context = ContextMgt.GetInstance();
            new ArticleTypeTableAdapter().Fill(context.ArticleType);
            var list = (from s in context.ArticleType
                        where s.ParentID == 5
                        orderby s.Seq
                        select s).ToList();
            return View(list);

        }

        public ActionResult InfoSub(int id)
        {
            if (Session["User"] == null)
                return this.RedirectToAction("../Admin/Login");

            MenuModel menu = Session["Menu"] as MenuModel;
            menu.Select("行业资信");

            SubTypeEditModel model = new SubTypeEditModel();

            var context = ContextMgt.GetInstance();
            new ArticleTableAdapter().Fill(context.Article);
            new ArticleTypeTableAdapter().Fill(context.ArticleType);
            var list = (from s in context.Article
                        where s.TypeID == id
                        orderby s.Seq
                        select s).ToList();
            var articleType = (from s in context.ArticleType
                               where s.ID == id
                               orderby s.Seq
                               select s).SingleOrDefault();
            if (articleType == null)
            {
                return this.RedirectToAction("Error", "Admin", new { message = "没有找到子分栏！" });
            }
            model.List = list;
            model.Type = articleType.ParentID;
            model.SubType = id;
            model.TypeName = articleType.Name;
            return View(model);

        }

        public ActionResult BaseInfo()
        {
            if (Session["User"] == null)
                return this.RedirectToAction("../Admin/Login");

            MenuModel menu = Session["Menu"] as MenuModel;
            menu.Select("基础资料");

            var context = ContextMgt.GetInstance();
            new BaseDataTableAdapter().Fill(context.BaseData);
            BaseInfoModel model = new BaseInfoModel();
            var company = (from p in context.BaseData
                           where p.Name == "Company"
                           select p).SingleOrDefault();
            if (company == null || company.IsValueNull())
                model.Company = string.Empty;
            else
                model.Company = company.Value;

            var address = (from p in context.BaseData
                           where p.Name == "Address"
                           select p).SingleOrDefault();
            if (address == null || address.IsValueNull())
                model.Address = string.Empty;
            else
                model.Address = address.Value;

            var phone = (from p in context.BaseData
                         where p.Name == "Phone"
                         select p).SingleOrDefault();
            if (phone == null || phone.IsValueNull())
                model.Phone = string.Empty;
            else
                model.Phone = phone.Value;

            var companyShort = (from p in context.BaseData
                                where p.Name == "CompanyShort"
                                select p).SingleOrDefault();
            if (companyShort == null || companyShort.IsValueNull())
                model.CompanyShort = string.Empty;
            else
                model.CompanyShort = companyShort.Value;

            return View(model);
        }

        [HttpPost]
        public ActionResult BaseInfo(BaseInfoModel model)
        {
            if (model.Password != null)
            {
                if (model.PasswordRep == null)
                    ModelState.AddModelError("PasswordRep", "请输入管理员密码（重复）");
                else if (model.Password != model.PasswordRep)
                    ModelState.AddModelError("Password", "两次密码不相同");
            }
            if (ModelState.IsValid)
            {
                try
                {
                    var context = ContextMgt.GetInstance();
                    new BaseDataTableAdapter().Fill(context.BaseData);

                    if (model.Password != null)
                    {
                        var password = (from p in context.BaseData
                                        where p.Name == "Password"
                                        select p).SingleOrDefault();
                        if (password == null)
                        {
                            password = context.BaseData.NewBaseDataRow();
                            password.Name = "Password";
                            context.BaseData.AddBaseDataRow(password);
                        }
                        password.Value = model.Password;
                    }

                    var company = (from p in context.BaseData
                                   where p.Name == "Company"
                                   select p).SingleOrDefault();
                    if (company == null)
                    {
                        company = context.BaseData.NewBaseDataRow();
                        company.Name = "Company";
                        context.BaseData.AddBaseDataRow(company);
                    }
                    if (model.Company != null)
                        company.Value = model.Company;
                    else
                        company.Value = string.Empty;

                    var address = (from p in context.BaseData
                                   where p.Name == "Address"
                                   select p).SingleOrDefault();
                    if (address == null)
                    {
                        address = context.BaseData.NewBaseDataRow();
                        address.Name = "Address";
                        context.BaseData.AddBaseDataRow(address);
                    }
                    if (model.Address != null)
                        address.Value = model.Address;
                    else
                        address.Value = string.Empty;


                    var phone = (from p in context.BaseData
                                 where p.Name == "Phone"
                                 select p).SingleOrDefault();
                    if (phone == null)
                    {
                        phone = context.BaseData.NewBaseDataRow();
                        phone.Name = "Phone";
                        context.BaseData.AddBaseDataRow(phone);
                    }
                    if (model.Phone != null)
                        phone.Value = model.Phone;
                    else
                        phone.Value = string.Empty;


                    var companyShort = (from p in context.BaseData
                                        where p.Name == "CompanyShort"
                                        select p).SingleOrDefault();
                    if (companyShort == null)
                    {
                        companyShort = context.BaseData.NewBaseDataRow();
                        companyShort.Name = "CompanyShort";
                        context.BaseData.AddBaseDataRow(companyShort);
                    }
                    if (model.CompanyShort != null)
                        companyShort.Value = model.CompanyShort;
                    else
                        companyShort.Value = string.Empty;

                    new BaseDataTableAdapter().Update(context.BaseData);

                    BaseModel.SAddress = model.Address;
                    BaseModel.SCompany = model.Company;
                    BaseModel.SPhone = model.Phone;

                    return this.RedirectToAction("Error", "Admin", new { message = "操作成功" });
                }
                catch (Exception e)
                {
                    return this.RedirectToAction("Error", "Admin", new { message = e.Message });
                }
            }
            return View(model);
        }

        public ActionResult HomeTeam()
        {
            if (Session["User"] == null)
                return this.RedirectToAction("../Admin/Login");

            MenuModel menu = Session["Menu"] as MenuModel;
            menu.Select("管理团队");

            var context = ContextMgt.GetInstance();
            new TeamTableAdapter().Fill(context.Team);
            var list = (from s in context.Team
                        orderby s.Seq
                        select s).ToList();
            return View(list);

        }

        public ActionResult HomeTeamEdit(int? id)
        {
            HomeTeamModel model = new HomeTeamModel();
            var context = ContextMgt.GetInstance();

            if (id != null)
            {
                new TeamTableAdapter().Fill(context.Team);

                var team = (from a in context.Team
                            where a.ID == id
                            select a).SingleOrDefault();
                if (team == null)
                {
                    return this.RedirectToAction("Error", "Admin", new { message = "没有找到团队成员！" });
                }
                model.Name = team.Name;
                model.Seq = team.Seq;
                model.ID = team.ID;
                model.ImagePath = team.ImagePath;
            }
            else
            {
                model.ID = -1;
            }

            return View(model);
        }

        [HttpPost]
        public ActionResult HomeTeamEdit(HomeTeamModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    GrandMan.GMDataSet.TeamRow team = null;
                    var context = ContextMgt.GetInstance();

                    if (model.ID == -1)
                    {
                        team = context.Team.NewTeamRow();
                        context.Team.AddTeamRow(team);
                    }
                    else
                    {
                        new TeamTableAdapter().Fill(context.Team);
                        team = (from a in context.Team
                                where a.ID == model.ID
                                select a).SingleOrDefault();
                        if (team == null)
                        {
                            return this.RedirectToAction("Error", "Admin", new { message = "没有找到团队成员！" });
                        }
                    }
                    team.Name = model.Name;
                    team.Seq = model.Seq;
                    team.ImagePath = model.ImagePath;

                    new TeamTableAdapter().Update(context.Team);
                    context.Team.AcceptChanges();

                    return this.RedirectToAction("HomeTeam", "Admin");
                }
                catch (Exception e)
                {
                    return this.RedirectToAction("Error", "Admin", new { message = e.Message });
                }
            }
            return View(model);
        }

        public ActionResult HomeTeamDelete(string ids)
        {
            string[] temp = ids.Split('!');
            int[] idList = new int[temp.Length];
            for (int i = 0; i < temp.Length; i++)
                idList[i] = Convert.ToInt32(temp[i]);
            try
            {
                var adapter = new TeamTableAdapter();
                foreach (var item in idList)
                    adapter.Delete(item);
            }
            catch (Exception e)
            {
                return this.RedirectToAction("Error", "Admin", new { message = e.Message });
            }

            return this.RedirectToAction("HomeTeam", "Admin");
        }

        public ActionResult HomePartner()
        {
            if (Session["User"] == null)
                return this.RedirectToAction("../Admin/Login");

            MenuModel menu = Session["Menu"] as MenuModel;
            menu.Select("服务品牌");

            var context = ContextMgt.GetInstance();
            new PartnerTableAdapter().Fill(context.Partner);
            var list = (from s in context.Partner
                        orderby s.Seq
                        select s).ToList();
            return View(list);

        }

        public ActionResult HomePartnerEdit(int? id)
        {
            HomePartnerModel model = new HomePartnerModel();
            var context = ContextMgt.GetInstance();

            if (id != null)
            {
                new PartnerTableAdapter().Fill(context.Partner);

                var team = (from a in context.Partner
                            where a.ID == id
                            select a).SingleOrDefault();
                if (team == null)
                {
                    return this.RedirectToAction("Error", "Admin", new { message = "没有找到服务品牌！" });
                }
                model.Name = team.Name;
                model.Seq = team.Seq;
                model.ID = team.ID;
                model.ImagePath = team.ImagePath;
            }
            else
            {
                model.ID = -1;
            }

            return View(model);
        }

        [HttpPost]
        public ActionResult HomePartnerEdit(HomePartnerModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    GrandMan.GMDataSet.PartnerRow team = null;
                    var context = ContextMgt.GetInstance();

                    if (model.ID == -1)
                    {
                        team = context.Partner.NewPartnerRow();
                        context.Partner.AddPartnerRow(team);
                    }
                    else
                    {
                        new PartnerTableAdapter().Fill(context.Partner);
                        team = (from a in context.Partner
                                where a.ID == model.ID
                                select a).SingleOrDefault();
                        if (team == null)
                        {
                            return this.RedirectToAction("Error", "Admin", new { message = "没有找到服务品牌！" });
                        }
                    }
                    team.Name = model.Name;
                    team.Seq = model.Seq;
                    team.ImagePath = model.ImagePath;

                    new PartnerTableAdapter().Update(context.Partner);
                    context.Partner.AcceptChanges();

                    return this.RedirectToAction("HomePartner", "Admin");
                }
                catch (Exception e)
                {
                    return this.RedirectToAction("Error", "Admin", new { message = e.Message });
                }
            }
            return View(model);
        }

        public ActionResult HomePartnerDelete(string ids)
        {
            string[] temp = ids.Split('!');
            int[] idList = new int[temp.Length];
            for (int i = 0; i < temp.Length; i++)
                idList[i] = Convert.ToInt32(temp[i]);
            try
            {
                var adapter = new PartnerTableAdapter();
                foreach (var item in idList)
                    adapter.Delete(item);
            }
            catch (Exception e)
            {
                return this.RedirectToAction("Error", "Admin", new { message = e.Message });
            }

            return this.RedirectToAction("HomePartner", "Admin");
        }
    }
}
